package com.missfresh.cooperation2meituan.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.missfresh.cooperation2meituan.vo.SysOrder;

public class HttpclientUtil {
    public static String post(String url,Object json) throws UnsupportedEncodingException{
    	
    	HttpClient client = new HttpClient();
    	PostMethod method = new PostMethod(url);
    	RequestEntity se = null;
    	if(json instanceof JSONObject){
    		se = new StringRequestEntity(((JSONObject)json).toJSONString(), "application/json", "UTF-8");
    	}
    	if(json instanceof JSONArray){
    		se = new StringRequestEntity(((JSONArray) json).toJSONString(), "application/json", "UTF-8");
    	}
    	method.setRequestEntity(se);
    	method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
    	method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 1000);

    	HttpMethodParams param = method.getParams();
    	param.setContentCharset("UTF-8");  
    	StringBuffer buf = new StringBuffer(); 
    	try {
			client.executeMethod(method);
			System.out.println(method.getStatusLine());  
			
			InputStream stream = method.getResponseBodyAsStream(); 
			BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
			
			String line;
			while (null != (line = br.readLine())) {
				buf.append(line).append("\n"); 
			}
			method.releaseConnection();  
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return buf.toString();
    }
    public static String ConvertToString(InputStreamReader inputStreamReader){  
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
        StringBuilder result = new StringBuilder();  
        String line = null;  
        try {  
            while((line = bufferedReader.readLine()) != null){  
                result.append(line + "\n");  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try{  
                inputStreamReader.close();  
                bufferedReader.close();  
            }catch(IOException e){  
                e.printStackTrace();  
            }  
        }  
        return result.toString();  
    }  
    public static void main(String[] args) {
    	noarray();
    	//arrayM();
    }
    public static void noarray() {
        //发送 GET 请求
    	try {
    	SysOrder order = new SysOrder();
    	order.setOuter_tid("123456789");
        String param = JsonUtil.obj2json(order);
        System.out.println(param);
    	String url="http://ma.staging.missfresh.cn/open_api/v1/orders";
    	String s;
		s = HttpclientUtil.post(url, param);
		System.out.println(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    
    
    public static void arrayM() {
    	//发送 GET 请求   优惠码
//    	String url="http://localhost:9999/couponController/arrayRout.do";
//    	String params = "[{name:'lz',code:'456',start_time:'2015-11-12 00:00:00',end_time:'2015-11-12 00:00:00',voucher_id:11}]";
    	
//    	String url="http://localhost:9999/couponController/batchInsertVoucher.do";
//    	String params = "["
//    			+ "{name:'vip11券',internal_id:'vip-92',limitnum:1,voucher_type_id:2,content:'好吃就送',description:'好吃就送',datenum:0,date_active:0,original_cost:0,preferential_price:80000,active:0,start_time:'2015-11-10 00:00:00',end_time:'2015-11-20 00:00:00',product_id:2,from_channel:'missfresh',black_product_pool_id: 3}"
//    			+ ",{name:'vip22券',internal_id:'vip-93',limitnum:1,voucher_type_id:2,content:'好吃就送',description:'好吃就送',datenum:0,date_active:0,original_cost:0,preferential_price:80000,active:0,start_time:'2015-11-10 00:00:00',end_time:'2015-11-20 00:00:00',product_id:2,from_channel:'missfresh',black_product_pool_id: 3}"
//    			+ "]";
    	
//    	String url="http://localhost:9999/couponController/batchInsertVoucherType.do";
//    	String params = "["
//    			+ "{name:'宝贝',internal_id:'vip-92'}"
//    			+ ",{name:'满减',internal_id:'vip-93'}"
//    			+ "]";
//    	
//    	String s;
    	
    	String url="http://localhost:8080/couponController/newUserVoucher.do";
    	String params = "{operation:'add',name:'bb',plant_form:'ALL',items:["
    			+ "{quantity:2,voucher_id:21}"
    			+ ",{quantity:2,voucher_id:31}"
    			+ "]}";
    	
//    	String params = "{operation:'update',id:4,name:'bb',plant_form:'ALL',items:["
//    			+ "{quantity:2,voucher_id:21}"
//    			+ ",{quantity:2,voucher_id:31}"
//    			+ "]}";
    	
    	String s;
    	try {
    		//s = HttpUtil.post(url, JSONObject.parseArray(params));
    		s = HttpclientUtil.post(url, JSONObject.parseObject(params));
    		System.out.println(s);
    	} catch (UnsupportedEncodingException e) {
    		e.printStackTrace();
    	}
    }
}
